import { defHttp } from '../utils/http/axios';
import { getRefreshToken } from '../utils/auth';
import type { TentantNameVO } from './model/loginModel';
import { ErrorMessageMode } from '../utils/http/axios';
import { withLoading } from '../utils/http/http';

enum Api {
  Login = '/admin-api/system/auth/login',
  RefreshToken = '/admin-api/system/auth/refresh-token?refreshToken=',
  GetTenantIdByName = '/admin-api/system/tenant/get-id-by-name?name=',
  LoginOut = '/admin-api/system/auth/logout',
  GetUserInfo = '/admin-api/system/auth/get-permission-info',
  SendSmsCode = '/admin-api/system/auth/send-sms-code',
  GetCaptcha = '/admin-api/system/captcha/get',
  CheckCaptcha = '/admin-api/system/captcha/check',
}

// 登录接口
export function login(data: Record<string, any>) {
  return withLoading(() =>
    defHttp.post<any>(
      {
        url: Api.Login,
        data,
      },
      {
        isReturnNativeResponse: true,
        joinPrefix: false,
      }
    )
  );
}

// 刷新访问令牌
export function refreshToken() {
  const refreshToken: string = getRefreshToken();
  return withLoading(() =>
    defHttp.post<any>(
      {
        url: Api.RefreshToken + refreshToken
      },
      {
        joinPrefix: false,
      }
    )
  );
}

// 获取用户权限信息
export function getUserInfo() {
  return withLoading(() =>
    defHttp.get<any>(
      {
        url: Api.GetUserInfo
      },
      {
        joinPrefix: false,
      }
    )
  );
}

// 登出
export function loginOut() {
  return withLoading(() =>
    defHttp.post<any>(
      {
        url: Api.LoginOut
      },
      {
        joinPrefix: false,
      }
    )
  );
}

// 使用租户名，获得租户编号
export function getTenantIdByName(name: string) {
  return withLoading(() =>
    defHttp.get<TentantNameVO>(
      {
        url: Api.GetTenantIdByName + name
      },
      {
        joinPrefix: false,
      }
    )
  );
}

// 使用租户域名，获得租户信息
export function getTenantByWebsite(website: string) {
  return withLoading(() =>
    defHttp.get<any>(
      {
        url: `/admin-api/system/tenant/get-by-website?website=${website}`
      },
      {
        joinPrefix: false,
      }
    )
  );
}

// 获取登录验证码
export function sendSmsCode(mobile: string, scene?: string) {
  return withLoading(() =>
    defHttp.post<any>(
      {
        url: Api.SendSmsCode,
        data: {
          mobile,
          scene,
        },
      },
      {
        joinPrefix: false,
      }
    )
  );
}

// 获取验证图片及token
export function getCaptcha(data: any) {
  console.log('调用获取验证码接口:', Api.GetCaptcha, data);
  return withLoading(() =>
    defHttp.post<any>(
      {
        url: Api.GetCaptcha,
        data,
        timeout: 15000,
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json, text/plain, */*'
        }
      },
      {
        isReturnNativeResponse: true,
        joinPrefix: false,
        errorMessageMode: ErrorMessageMode.NONE
      }
    )
  );
}

// 滑动或者点选验证
export function checkCaptcha(data: any) {
  console.log('调用验证码校验接口:', Api.CheckCaptcha, data);
  return withLoading(() =>
    defHttp.post<any>(
      {
        url: Api.CheckCaptcha,
        data,
        headers: {
          'Content-Type': 'application/json'
        }
      },
      {
        isReturnNativeResponse: true,
        joinPrefix: false,
        errorMessageMode: ErrorMessageMode.NONE
      }
    )
  );
}
